/**
 * Created by zoushuiyun
 */
import { OnInit, Injectable } from '@angular/core';

/**
 * 缓存服务  将ajax请求的一些状态数据存储到localStorage中，避免系统中频繁请求提升用户体验
 */
@Injectable()
export class CacheService implements OnInit {


  static USER_TOKEN_KEY = 'USER_TOKEN';
  static FIRST_URL = 'FIRST_URL';
  static CURR_USER_KEY = 'USER';
  static CURR_USER_INFO_KEY = 'USER_INFO';

  static APP_INFO_KEY = 'APP_INFO';
  static AGENT_LEVEL_KEY = 'AGENT_LEVEL';//渠道级别
  static GOODS_CLASS_KEY = 'GOODS_CLASS';//商品类别
  static DISTRICT_KEY = 'DISTRICT_LIST';//业务员运营中心
  static MARKET_AGENT_LEVELS_KEY = 'MARKET_AGENT_LEVELS';//意向用户加盟类别
  static SALESMAN_TYPE_KEY = 'SALESMAN_TYPE';//门店、代理类别
  static ACTIVITY_PRODUCTS_KEY = 'ACTIVITY_PRODUCTS';//活动产品


  ngOnInit(): void {
    if (typeof window.localStorage === 'undefined') {
      alert('请使用chrome或更高版本的IE浏览器');
    }
  }

  private add(key, obj) {
    if (obj) {
      if (typeof obj === 'object') {
        // storage中只能存储字符串，所以将json对象转换成字符串
        const objStr = JSON.stringify(obj);
        window.localStorage.setItem(key, objStr);
      } else {
        window.localStorage.setItem(key, obj);
      }
    }
    if(obj == null){
      window.localStorage.setItem(key, null);
    }
  }

  private get(key) {
    const objStr = window.localStorage.getItem(key);
    if (objStr) {
      try {
        // 重新转换为对象
        return JSON.parse(objStr);
      } catch (err) {

      }
    }
    return objStr;
  }

  clear() {
    window.localStorage.removeItem(CacheService.USER_TOKEN_KEY);
    window.localStorage.removeItem(CacheService.FIRST_URL);
    window.localStorage.removeItem(CacheService.CURR_USER_KEY);
    window.localStorage.removeItem(CacheService.CURR_USER_INFO_KEY);
    window.localStorage.removeItem(CacheService.APP_INFO_KEY);
    window.localStorage.removeItem(CacheService.AGENT_LEVEL_KEY);
    window.localStorage.removeItem(CacheService.GOODS_CLASS_KEY);
    window.localStorage.removeItem(CacheService.DISTRICT_KEY);
    window.localStorage.removeItem(CacheService.MARKET_AGENT_LEVELS_KEY);
    window.localStorage.removeItem(CacheService.SALESMAN_TYPE_KEY);
    window.localStorage.removeItem(CacheService.ACTIVITY_PRODUCTS_KEY);

  }

  /**
   * token信息
   * @param currUser
   */
  setTokenInfo(token: any) {
    this.add(CacheService.USER_TOKEN_KEY, token);
  }

  /**
   *  token信息
   * @returns {any|any}
   */
  getTokenInfo(): any {
    return this.get(CacheService.USER_TOKEN_KEY);
  }

  /**
   * 请求的url
   * @param currUser
   */
  setFirstUrl(url: any) {
    this.add(CacheService.FIRST_URL, url);
  }

  /**
   * 请求的url
   * @returns {any|any}
   */
  getFirstUrl(): any {
    return this.get(CacheService.FIRST_URL);
  }

  /**
   * 缓存用户数据详细数据
   * @param currUser
   */
  setCurrUserInfo(currUserInfo: any) {
    this.add(CacheService.CURR_USER_INFO_KEY, currUserInfo);
  }

  /**
   * 获取用户数据 详细数据
   * @returns {any|any}
   */
  getCurrUserInfo(): any {
    return this.get(CacheService.CURR_USER_INFO_KEY);
  }


  /**
   * 缓存用户数据 对应currUserService中对象
   * @param currUser
   */
  setCurrUser(currUser: any) {
    this.add(CacheService.CURR_USER_KEY, currUser);
  }

  /**
   * 获取用户数据 对应currUserService中对象
   * @returns {any|any}
   */
  getCurrUser(): any {
    return this.get(CacheService.CURR_USER_KEY);
  }

  /**
   * 缓存app_info
   * @param currUser
   */
  setAppInfo(appInfo: any) {
    this.add(CacheService.APP_INFO_KEY, appInfo);
  }

  /**
   * 获取缓存app_info
   * @returns {any|any}
   */
  getAppInfo(): any {
    return this.get(CacheService.APP_INFO_KEY);
  }

  /**
   * 缓存渠道级别信息
   * @param currUser
   */
  setAgentLevel(agentLevel: any) {
    this.add(CacheService.AGENT_LEVEL_KEY, agentLevel);
  }

  /**
   * 获取渠道级别信息
   * @returns {any|any}
   */
  getAgentLevel(): any {
    return this.get(CacheService.AGENT_LEVEL_KEY);
  }

  /**
   * 缓存商品分类信息
   * @param currUser
   */
  setGoodsClass(goodsClass: any) {
    this.add(CacheService.GOODS_CLASS_KEY, goodsClass);
  }

  /**
   * 获取商品分类信息
   * @returns {any|any}
   */
  getGoodsClass(): any {
    return this.get(CacheService.GOODS_CLASS_KEY);
  }

  /**
   * 缓存业务员运营中心信息
   * @param currUser
   */
  setDistrict(districts: any) {
    this.add(CacheService.DISTRICT_KEY, districts);
  }

  /**
   * 获取业务员运营中心信息
   * @returns {any|any}
   */
  getDistrict(): any {
    return this.get(CacheService.DISTRICT_KEY);
  }

  /**
   * 缓存意向用户加盟类别信息
   * @param currUser
   */
  setMarketAgentLevel(level: any) {
    this.add(CacheService.MARKET_AGENT_LEVELS_KEY, level);
  }

  /**
   * 获取意向用户加盟类别信息
   * @returns {any|any}
   */
  getMarketAgentLevel(): any {
    return this.get(CacheService.MARKET_AGENT_LEVELS_KEY);
  }

  /**
   * 缓存门店、代理类别
   * @param currUser
   */
  setSalesmanType(types: any) {
    this.add(CacheService.SALESMAN_TYPE_KEY, types);
  }

  /**
   * 获取门店、代理类别
   * @returns {any|any}
   */
  getSalesmanType(): any {
    return this.get(CacheService.SALESMAN_TYPE_KEY);
  }

  /**
   * 缓存活动产品
   * @param currUser
   */
  setActivityProducts(datas: any) {
    this.add(CacheService.ACTIVITY_PRODUCTS_KEY, datas);
  }

  /**
   * 获取活动产品
   * @returns {any|any}
   */
  getActivityProducts(): any {
    return this.get(CacheService.ACTIVITY_PRODUCTS_KEY);
  }

}
